package com.voice.call.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.entity.VosAccount;
import com.voice.call.service.VosService;
import com.voice.call.version.ApiVersion;

import voice.voice.call.cache.ConfigVosAccountCache;



@RestController
@RequestMapping("/api/{version}/vos")
public class VosController {
	
	private static final Logger log = LoggerFactory.getLogger(VosController.class);
	
	@Resource
	private ConfigVosAccountCache configVosAccountCache;
	
	@Resource
	private VosService  vosService;
	
	//创建vos帐号
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "/create", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result createVosAccount(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		
		//参数验证
		String lang = ConstLang.getLang(params);
		if (params == null || params.get("phone")==null || params.get("billAccount")==null) {
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		//getVosAccountForBillAccount
		String phone = params.get("phone").toString().trim();
		String account = params.get("billAccount").toString().trim(); //vos计费帐号
		String displayNumber = params.get("displayNumber")!=null?params.get("displayNumber").toString().trim():"";
		
		VosAccount acct = configVosAccountCache.getVosAccountForBillAccount(account);
		if (acct==null) {
			log.info("计费帐号有错");
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Map<String, Object> rsMap=vosService.getVosAccount(acct, phone, displayNumber);
		return new Result(rsMap);
	}
	
	
	//删除vos帐号
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result deleteVosAccount(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		
		//参数验证
		String lang = ConstLang.getLang(params);
		if (params == null || params.get("phone")==null || params.get("billAccount")==null) {
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		String phone = params.get("phone").toString().trim();
		String account = params.get("billAccount").toString().trim(); //vos计费帐号
		VosAccount acct = configVosAccountCache.getVosAccountForBillAccount(account);
		if (acct==null) {
			log.info("计费帐号有错");
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		if(vosService.deletePhone(acct, phone)) {
			return new Result();
		}
		return new Result(ResultEnum.FAILED);
	}
	
	//查询vos帐号
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result queryVosAccount(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		
		//参数验证
		String lang = ConstLang.getLang(params);
		if (params == null || params.get("phone")==null || params.get("billAccount")==null) {
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		String phone = params.get("phone").toString().trim();
		String account = params.get("billAccount").toString().trim(); //vos计费帐号
		VosAccount acct = configVosAccountCache.getVosAccountForBillAccount(account);
		if (acct==null) {
			log.info("计费帐号有错");
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Map<String, Object> rsMap=vosService.queryPhone(acct, phone, null);
		return new Result(rsMap);
	}
  
}
